#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus.h>

// 定义消息处理函数
DBusHandlerResult dbus_message_handler(DBusConnection* conn, DBusMessage* msg, void* user_data)
{
    if (dbus_message_is_method_call(msg, "com.example.Calculator", "Add")) {
        // 处理 Add 方法
        DBusMessageIter iter;
        int a, b, sum;
        dbus_message_iter_init(msg, &iter);
        dbus_message_iter_get_basic(&iter, &a);
        dbus_message_iter_next(&iter);
        dbus_message_iter_get_basic(&iter, &b);
        sum = a + b;
        DBusMessage* reply = dbus_message_new_method_return(msg);
        DBusMessageIter reply_iter;
        dbus_message_iter_init_append(reply, &reply_iter);
        dbus_message_iter_append_basic(&reply_iter, DBUS_TYPE_INT32, &sum);
        dbus_connection_send(conn, reply, NULL);
        dbus_message_unref(reply);
        return DBUS_HANDLER_RESULT_HANDLED;
    }
    else if (dbus_message_is_method_call(msg, "com.example.Calculator", "Quit")) {
        // 处理 Quit 方法
        dbus_connection_close(conn);
        return DBUS_HANDLER_RESULT_HANDLED;
    }
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

// 定义 dbus 对象路径表
static const DBusObjectPathVTable dbus_interface = {
    .message_function = dbus_message_handler,  // 注册消息处理函数
    //.destroy_function = NULL                   // 可选，销毁时的回调函数
};

// 回调函数，处理异步应答
void dbus_reply_handler(DBusPendingCall* pending, void* user_data)
{
    int* sum = (int*)user_data;
    DBusMessage* reply = dbus_pending_call_steal_reply(pending);
    DBusMessageIter iter;
    if (reply)
    {
        if (dbus_message_iter_init(reply, &iter))
        {
            dbus_message_iter_get_basic(&iter, sum);
            printf("The sum is: %d\n", *sum);
        }
        dbus_message_unref(reply);
    }
    dbus_pending_call_unref(pending);
}

int main()
{
    DBusError error;
    DBusConnection* conn;
    DBusMessage* msg;
    DBusPendingCall* pending;
    int sum = 0;

    dbus_error_init(&error);

    // 建立到系统DBus总线的连接
    conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);

    if (dbus_error_is_set(&error)) {
        fprintf(stderr, "Error connecting to system bus: %s\n", error.message);
        dbus_error_free(&error);
        return EXIT_FAILURE;
    }

    // 注册对象路径和消息处理函数
    dbus_bus_request_name(conn, "com.example.Calculator", 0, &error);

    if (dbus_error_is_set(&error)) {
        fprintf(stderr, "Error requesting name: %s\n", error.message);
        dbus_error_free(&error);
        return EXIT_FAILURE;
    }

    // 注册对象路径
    dbus_connection_register_object_path(conn, "/com/example/Calculator", &dbus_interface, NULL);

    // 创建DBus消息
    msg = dbus_message_new_method_call("com.example.Calculator", "/com/example/Calculator", "com.example.Calculator", "Add");

    // 将参数添加到DBus消息中
    DBusMessageIter iter;
    dbus_message_iter_init_append(msg, &iter);
    int a = 2, b = 3;
    dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &a);
    dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &b);

    // 发送DBus消息并等待应答
    dbus_connection_send_with_reply(conn, msg, &pending, -1);
    dbus_message_unref(msg);

    dbus_pending_call_set_notify(pending, dbus_reply_handler, &sum, NULL);

    while (dbus_connection_read_write_dispatch(conn, -1)) {
        // 事件循环，处理消息
    }

    dbus_connection_unref(conn);

    return EXIT_SUCCESS;
}
